<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.7"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>cpp-ipfs-http-client: threading_example.cc</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">cpp-ipfs-http-client
   </div>
   <div id="projectbrief">IPFS C++ client library</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.7 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>

<div class="header">
  <div class="headertitle"><div class="title">threading_example.cc</div></div>
</div><!--header-->
<div class="contents">
<p>An example of how to use IPFS Client with threads, using the Abort() and Reset() methods.</p>
<p>An example of how to use IPFS Client with threads, using the Abort() and Reset() methods.</p>
<div class="fragment"><div class="line"> </div>
<div class="line"><span class="preprocessor">#include &lt;ipfs/client.h&gt;</span></div>
<div class="line"><span class="comment">// Or when you use IPFS locally:</span></div>
<div class="line"><span class="comment">//#include &quot;ipfs/client.h&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;sstream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;thread&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> startThread() {</div>
<div class="line">  <span class="keywordflow">try</span> {</div>
<div class="line">    <span class="comment">// Create IPFS Client object, with 2 minutes time-out.</span></div>
<div class="line">    <a id="_a0" name="_a0"></a><a class="code hl_class" href="classipfs_1_1Client.html">ipfs::Client</a> client(<span class="stringliteral">&quot;localhost&quot;</span>, 5001, <span class="stringliteral">&quot;2m&quot;</span>);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// Only start a single thread</span></div>
<div class="line">    std::thread thread([&amp;client]() {</div>
<div class="line">      std::stringstream contents;</div>
<div class="line">      <span class="keywordflow">try</span> {</div>
<div class="line">        <span class="comment">// File should not exists, takes forever (until time-out)</span></div>
<div class="line">        client.<a id="a1" name="a1"></a><a class="code hl_function" href="classipfs_1_1Client.html#a7598eaecec5293f29f84e2fc7ca7bc5b">FilesGet</a>(<span class="stringliteral">&quot;QmZp1rrtGTictR2rpNcx4673R7qU9Jdr9DQ6Z7F6Wgo2bQ&quot;</span>,</div>
<div class="line">                        &amp;contents);</div>
<div class="line"> </div>
<div class="line">        <span class="comment">// Code below will never be reached, since we abort the request</span></div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;Output: &quot;</span> &lt;&lt; contents.str() &lt;&lt; std::endl;</div>
<div class="line">      } <span class="keywordflow">catch</span> (<span class="keyword">const</span> std::runtime_error&amp; e) {</div>
<div class="line">        <span class="comment">// Run-time error will be thrown because of the aborting request.</span></div>
<div class="line">        std::cerr &lt;&lt; <span class="stringliteral">&quot;Error: &quot;</span> &lt;&lt; e.what() &lt;&lt; std::endl;</div>
<div class="line">      }</div>
<div class="line">    });</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">if</span> (thread.joinable()) {</div>
<div class="line">      std::cout &lt;&lt; <span class="stringliteral">&quot;Directly try to abort the request and stop the thread.&quot;</span></div>
<div class="line">                &lt;&lt; std::endl;</div>
<div class="line">      <span class="comment">// Try to remove the Abort() and Reset() calls,</span></div>
<div class="line">      <span class="comment">// and see the difference yourself :)</span></div>
<div class="line">      client.<a id="a2" name="a2"></a><a class="code hl_function" href="classipfs_1_1Client.html#aaa862322e6ec703b91ed356d968fbc49">Abort</a>();</div>
<div class="line">      thread.join(); <span class="comment">// Should not be blocking now</span></div>
<div class="line">      client.<a id="a3" name="a3"></a><a class="code hl_function" href="classipfs_1_1Client.html#a60b2274e7ee70ed9392870cd2c5a8559">Reset</a>();</div>
<div class="line">    }</div>
<div class="line"> </div>
<div class="line">  } <span class="keywordflow">catch</span> (<span class="keyword">const</span> std::exception&amp; e) {</div>
<div class="line">    std::cerr &lt;&lt; <span class="stringliteral">&quot;General error: &quot;</span> &lt;&lt; e.what() &lt;&lt; std::endl;</div>
<div class="line">  }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main() {</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Starting thread..&quot;</span> &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// Start a request inside a thread</span></div>
<div class="line">  startThread();</div>
<div class="line"> </div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Done!&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">  <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
<div class="ttc" id="aclassipfs_1_1Client_html"><div class="ttname"><a href="classipfs_1_1Client.html">ipfs::Client</a></div><div class="ttdoc">IPFS client.</div><div class="ttdef"><b>Definition</b> client.h:48</div></div>
<div class="ttc" id="aclassipfs_1_1Client_html_a60b2274e7ee70ed9392870cd2c5a8559"><div class="ttname"><a href="classipfs_1_1Client.html#a60b2274e7ee70ed9392870cd2c5a8559">ipfs::Client::Reset</a></div><div class="ttdeci">void Reset()</div><div class="ttdoc">Resets the abort call, allowing to execute new API requests again.</div><div class="ttdef"><b>Definition</b> client.cc:483</div></div>
<div class="ttc" id="aclassipfs_1_1Client_html_a7598eaecec5293f29f84e2fc7ca7bc5b"><div class="ttname"><a href="classipfs_1_1Client.html#a7598eaecec5293f29f84e2fc7ca7bc5b">ipfs::Client::FilesGet</a></div><div class="ttdeci">void FilesGet(const std::string &amp;path, std::iostream *response)</div><div class="ttdoc">Get a file from IPFS.</div><div class="ttdef"><b>Definition</b> client.cc:206</div></div>
<div class="ttc" id="aclassipfs_1_1Client_html_aaa862322e6ec703b91ed356d968fbc49"><div class="ttname"><a href="classipfs_1_1Client.html#aaa862322e6ec703b91ed356d968fbc49">ipfs::Client::Abort</a></div><div class="ttdeci">void Abort()</div><div class="ttdoc">Abort any current running IPFS API request.</div><div class="ttdef"><b>Definition</b> client.cc:476</div></div>
</div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.7
</small></address>
</body>
</html>
